package com.rcloud.server.sealtalk.controller;


import com.alipay.api.AlipayApiException;
import com.alipay.api.domain.AlipayTradeAppPayModel;
import com.ijpay.alipay.AliPayApi;
import com.ijpay.alipay.AliPayApiConfig;
import com.ijpay.alipay.AliPayApiConfigKit;
import com.rcloud.server.sealtalk.configuration.bean.AliPayBean;
import com.rcloud.server.sealtalk.constant.ErrorCode;
import com.rcloud.server.sealtalk.controller.base.AbstractAliPayApiController;
import com.rcloud.server.sealtalk.controller.param.PayParam;
import com.rcloud.server.sealtalk.exception.ServiceException;
import com.rcloud.server.sealtalk.model.dto.PayInfoDto;
import com.rcloud.server.sealtalk.model.response.APIResult;
import com.rcloud.server.sealtalk.model.response.APIResultWrap;
import com.rcloud.server.sealtalk.service.PayService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * <p>IJPay 让支付触手可及，封装了微信支付、支付宝支付、银联支付常用的支付方式以及各种常用的接口。</p>
 *
 * <p>不依赖任何第三方 mvc 框架，仅仅作为工具使用简单快速完成支付模块的开发，可轻松嵌入到任何系统里。 </p>
 *
 * <p>IJPay 交流群: 723992875</p>
 *
 * <p>Node.js 版: https://gitee.com/javen205/TNW</p>
 *
 * <p>支付宝支付 Demo</p>
 *
 * @author Javen
 */
@Api(tags = "支付宝支付接口")
@Slf4j
@RestController
@RequestMapping("/pay/aliPay")
@AllArgsConstructor
public class AliPayController extends AbstractAliPayApiController {

    private AliPayBean aliPayBean;

    private PayService payService;

    @Override
    public AliPayApiConfig getApiConfig() {
        AliPayApiConfig aliPayApiConfig;
        try {
            aliPayApiConfig = AliPayApiConfigKit.getApiConfig(aliPayBean.getAppId());
        } catch (Exception e) {
            aliPayApiConfig = AliPayApiConfig.builder()
                    .setAppId(aliPayBean.getAppId())
                    .setAliPayPublicKey(aliPayBean.getPublicKey())
                    .setCharset("UTF-8")
                    .setPrivateKey(aliPayBean.getPrivateKey())
                    .setServiceUrl(aliPayBean.getServerUrl())
                    .setSignType("RSA2")
                    .build();
        }
        return aliPayApiConfig;
    }

    /**
     * app支付
     */
    @PostMapping(value = "/payment")
    @ApiOperation(value = "支付宝APP支付，根据订单号进行支付", notes = "支付宝APP支付，根据订单号进行支付")
    public APIResult<Object> payment(@RequestBody PayParam payParam) throws ServiceException {
        //获取支付信息
        PayInfoDto payInfo = payService.pay(payParam);
        try {
            AlipayTradeAppPayModel model = new AlipayTradeAppPayModel();
            model.setBody(payInfo.getBody());
            model.setSubject(payInfo.getBody());
            model.setOutTradeNo(payInfo.getPayNo());
            model.setTimeoutExpress("30m");
            model.setTotalAmount(payInfo.getPayAmount().toString());
            model.setPassbackParams(payParam.getBusiType().toString());
            model.setProductCode("QUICK_MSECURITY_PAY");
            String orderInfo = AliPayApi.appPayToResponse(model, aliPayBean.getDomain() + "/notice/pay/ali/payNotify").getBody();
            return APIResultWrap.ok(orderInfo);
        } catch (AlipayApiException e) {
            throw new ServiceException(ErrorCode.PAY_INFO_ERROR, "system error:" + e.getMessage());
        }
    }
}